package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.TChoice;
import com.ruoyi.project.system.domain.TFillinganswer;
import com.ruoyi.project.system.service.ITFillinganswerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TFilling;
import com.ruoyi.project.system.service.ITFillingService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 填空题库Controller
 * 
 * @author ruoyi
 * @date 2020-05-09
 */
@RestController
@RequestMapping("/system/filling")
public class TFillingController extends BaseController
{
    @Autowired
    private ITFillingService tFillingService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ITFillinganswerService tFillinganswerService;

    /**
     * 查询填空题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:filling:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFilling tFilling)
    {
        startPage();
        List<TFilling> list = tFillingService.selectTFillingList(tFilling);
        return getDataTable(list);
    }

    /**
     * 导出填空题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:filling:export')")
    @Log(title = "填空题库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TFilling tFilling)
    {
        List<TFilling> list = tFillingService.selectTFillingList(tFilling);
        ExcelUtil<TFilling> util = new ExcelUtil<TFilling>(TFilling.class);
        return util.exportExcel(list, "filling");
    }

    /**
     * 获取填空题库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:filling:query')")
    @GetMapping(value = "/{fillingId}")
    public AjaxResult getInfo(@PathVariable("fillingId") Integer fillingId)
    {
        return AjaxResult.success(tFillingService.selectTFillingById(fillingId));
    }

    /**
     * 新增填空题库
     */
    @PreAuthorize("@ss.hasPermi('system:filling:add')")
    @Log(title = "填空题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TFilling tFilling)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        tFilling.setUserId(user.getUserId().intValue());
        tFillingService.insertTFilling(tFilling);
        System.out.println("tFilling = " + tFilling);
        return toAjax( tFillingService.addMiddle(tFilling.getCourseId(),tFilling.getFillingId()));
    }

    /**
     * 修改填空题库
     */
    @PreAuthorize("@ss.hasPermi('system:filling:edit')")
    @Log(title = "填空题库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFilling tFilling)
    {
        return toAjax(tFillingService.updateTFilling(tFilling));
    }

    /**
     * 删除填空题库
     */
    @PreAuthorize("@ss.hasPermi('system:filling:remove')")
    @Log(title = "填空题库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fillingIds}")
    public AjaxResult remove(@PathVariable Integer[] fillingIds)
    {
        return toAjax(tFillingService.deleteTFillingByIds(fillingIds));
    }
    @PostMapping("/filingAnswer")
    @ResponseBody
    public AjaxResult getMultiselChoiceList(Integer fllingId )
    {
        TFillinganswer tFillinganswer = new TFillinganswer();
        tFillinganswer.setFillingId(fllingId);
        List<TFillinganswer> list = tFillinganswerService.selectTFillinganswerList(tFillinganswer);
        if(list!=null&&list.size()>0){
            return AjaxResult.success(list);
        }else {
            return AjaxResult.error("操作失败");
        }
    }
}
